// pages/playlist/playlist.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    msg: '',
    songList:[],
    count: 12
  },
  toPlay: function (e) {
    var musicid = e.currentTarget.dataset.musicid
    var musicindex = e.currentTarget.dataset.musicindex
    console.log(musicindex);
    //idList是js数组类型
    var idList = this.data.idList
    //把数组转为json字符串
    idList = JSON.stringify(idList)
    console.log(idList)
    //路由跳转传参，路由中传递的参数都是字符串类型
    wx.navigateTo({
      url: '/pages/play/play?id=' + musicid + '&idList=' + idList + '&musicindex=' + musicindex,
    })
    // this.music.play()
    this.setData({
      playFlag: false
    })
  },
    //歌曲列表向上滚动时，滚动到底部触发加载事件
  lower: function () {
      var count = this.data.count + 6
      this.search(count)
  },
  iptblur: function (e) {
    this.setData({
      msg: e.detail.value
    })
  },
  searchMusic: function () {
    var count = this.data.count
    this.search(count)
  },
  search: function (count) {
    wx.request({
      url: 'https://music.163.com/api/search/get?s=' + this.data.msg + '&type=1&limit=' + count,
      success: (res) => {
        console.log(res.data);
        //歌手歌曲列表
        var songList = res.data.result.songs
        //收集所有歌曲id，放在一个数组中
        var idList = []
        var imgList = []
        for(var i = 0; i < songList.length; i++){
          idList.push(songList[i].id)
          wx.request({
            url: 'https://music.163.com/api/song/detail/?id=' + songList[i].id + '&ids=[' + songList[i].id + ']',
            success: (res) => {
              console.log(res.data)
              imgList.push(res.data.songs[0].album.picUrl)
      }
    })
  }
  wx.showToast({
    title: '数据加载中',
    icon: 'loading',
    duration: 2000
  })
  // 设置延时器，请求数据2秒钟后更改data中的内容
  setTimeout(()=>{
    this.setData({
      songList: songList,
      idList: idList,
      imgList: imgList,
      count: count,
    })
    }, 2000)
    //setInterval(() => {},1000)
  }
  })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})